<template>
  <div>
    <el-form label-width="120px" :model="form" ref="form" :rules="rulesForm">
      <el-form-item :label="$t('订单编号')">
        <div style="line-height: 32px">
          {{ rows.loanRecordId }}
        </div>
      </el-form-item>
      <el-form-item :label="$t('剩余应还金额')">
        <div style="line-height: 32px">
          {{ rows.notRefundAmount }}
        </div>
      </el-form-item>

      <el-form-item :label="$t('选择展期天数')" prop="rollOverDay">
        <el-select v-model="form.rollOverDay" @change="selectRollDay">
          <el-option v-for="item in rollOverNumList" :label="item.rollOverDay" :value="item.rollOverDay"
                     :key="item.rollOverDay"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('展期手续费')">
        <div style="line-height: 32px">
          {{ serviceFee }}
        </div>
      </el-form-item>
      <el-form-item :label="$t('还款银行卡')" prop="receiverBank">
        <el-select v-model="form.receiverBank">
          <el-option v-for="item in backList" :key="item.alias" :label="item.alias"
                     :value="item.alias"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('上传凭证')" v-if="$permiss[524]">
        <el-upload
            class="avatar-uploader"
            :show-file-list="false"
            ref="upload"
            action="#"
            accept=".jpg,.jpeg,.png,.gif,.bmp,.JPG,.JPEG"
            :http-request="uploadChange">
          <img v-if="form.credentials" :src="form.credentials" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item :label="$t('实际收款时间')" prop="overTime">
        <el-date-picker
            value-format="yyyy-MM-dd HH:mm:ss"
            v-model="form.overTime"
            type="datetime"
            :placeholder="$t('选择日期时间')">
        </el-date-picker>
      </el-form-item>
      <el-form-item :label="$t('实际收款金额')" prop="delayAmount">
        <el-input v-model="form.delayAmount"/>
      </el-form-item>
      <el-form-item :label="$t('交易流水')">
        <el-input v-model="form.transferNumber"></el-input>
      </el-form-item>
      <el-form-item :label="$t('备注')" prop="remark">
        <el-input type="textarea" :row="3" v-model="form.remark"></el-input>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import qs from "qs";
import {doRollOver,uploadRollOverCertificate} from '@/api/finance'
import {fetchRefundBank} from '@/api/pay'
import {selectRollOver} from '@/api/product'
export default {
  props: ['rows'],
  data() {
    return {
      form: {
        delayAmount: null
      },
      rulesForm: {
        rollOverDay: [
          {required: true, message: this.$t('请选择'),},
        ],
        receiverBank: [
          {required: true, message: this.$t('请选择'),},
        ],
        delayAmount: [
          {required: true, message: this.$t('请选择'),},
        ],
        overTime: [
          {required: true, message: this.$t('请选择'),},
        ]
      },
      backList: [],
      serviceFee: 0,
      rollOverNumList: []
    }
  },
  watch: {
    rows: {//深度监听
      immediate: true,
      handler(val) {
        this.getCollectionBack(val)
        this.getRollOverNum(val)
      },
      deep: true
    }
  },
  methods: {
    submitForm() {
      return new Promise((resolve) => {
        this.$refs.form.validate(async v => {
          if (v) {
            if (this.form.delayAmount < this.serviceFee) return this.$message.error(this.$t('实收展期手续费不足,无法展期'))
            this.$confirm(this.$t("为避免造成损失，请确认是否进行该操作") + '?', this.$t("提示"), {
              type: "warning",
            }).then(async () => {
              this.$emit('loading', true)
              const res = await doRollOver(
                  {
                    ...this.form,
                    loanRecordId: this.rows.loanRecordId
                  }
              )
              if (res.code === 1) {
                resolve(res.code)
                this.$emit('loading', false)
                this.$message.success(this.$t('操作成功'))
              } else {
                this.$emit('loading', false)
              }
            }).catch(() => {
            });
          }
        })
      })
    },
    async uploadChange(data) {
      const formData = new FormData()
      formData.append('file', data.file)
      formData.append('loanRecordId', this.rows.loanRecordId)
      const res = await uploadRollOverCertificate(formData)
      if (res.code === 1) {
        this.$set(this.form, 'credentials', res.data)
      }
    },
    selectRollDay(value) {
      const rollOver = this.rollOverNumList.find(item => item.rollOverDay === value)
      const {passAmount} = this.rows
      const free = rollOver.rollOverInterest * passAmount
      this.serviceFee = parseFloat(free.toFixed(2))
      this.form.delayAmount = this.serviceFee
    },
    clearForm() {
      this.serviceFee = 0
      this.form = {
        delayAmount: null
      }
      this.$refs.form.resetFields()
    },
    async getRollOverNum(params) {
      if (this.$permiss[293]) {
        const res = await selectRollOver({
          productName: params.productName
        })
        if (res.code === 1) {
          this.rollOverNumList = res.data
        }
      }
    },
    // 获取还款银行
    async getCollectionBack(row) {
      fetchRefundBank(row).then(res => {
        this.backList = res.data
        if (this.backList && this.backList.length) {
          this.$set(this.form, 'receiverBank', this.backList[0].alias)
        }
      })
    },
  }
}
</script>

<style scoped>
>>> .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

>>> .avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
